import psutil
import json
from app.machines.mac import MacInfo

class MemoryInfo:
    """
    内存信息
    """
    def get_memory_info():
        memory_dict = {}
        ## 物理内存信息
        svmem = psutil.virtual_memory()
        print(svmem)
        # 内存总共大小
        total_memory_size = round(svmem.total/1024/1024/1024,2)
        #内存可用量
        available_size = round(svmem.available/1024/1024/1024,2)
        # 内存使用率
        used_percent = svmem.percent
        # 内存已用量 与使用量相同
        used_size = round(svmem.used/1024/1024/1024,2)
        # 内存使用量 与已用量相同
        memory_usage = round(svmem.used/1024/1024/1024,2)
        # 备用或叫剩余量
        spare_size = round(svmem.free/1024/1024/1024,2)
        # if psutil.LINUX:
        #     # 活动-unix
        #     active = svmem.active
        #     # 非活动-unix
        #     inactive = svmem.inactive
        #     # 缓冲区-linux
        #     buffers = svmem.buffers
        #     # 缓存-linux
        #     cached = svmem.cached
        #     # 共享-linux
        #     sharde = svmem.shared
        #     # slab-linux
        #     slab = svmem.slab
        #     # 有线-mac
        #     wired = svmem.wired
        #     print(active)
        #     print(inactive)
        #     print(buffers)
        #     print(cached)
        #     print(sharde)
        #     print(slab)
        #     print(wired)
        #虚拟内存信息
        sswap = psutil.swap_memory()
        print(sswap)
        print(type(sswap))
        # 虚拟内存总共大小
        swap_memory_total = round(sswap.total/1024/1024/1024,2)
        # 虚拟内存使用率
        swap_used_percent = sswap.percent
        # 虚拟内存已用量 与虚拟内存使用量相同
        swap_used_size = round(sswap.used / 1024 / 1024 / 1024, 2)
        # 虚拟内存使用量 与虚拟内存已用量相同
        swap_memory_usage = round(sswap.used / 1024 / 1024 / 1024, 2)
        # 备用或叫剩余量
        swap_spare_size = round(sswap.free / 1024 / 1024 / 1024, 2)
        # 系统已从磁盘交换入的大小
        swap_sin = round(sswap.sin / 1024 / 1024 / 1024, 2)
        # 系统已从磁盘交换出的大小
        swap_sout = round(sswap.sout / 1024 / 1024 / 1024, 2)
        #-----------------------------------------------
        memory_dict['mac_code'] = MacInfo.get_mac()
        memory_dict['total_memory_size'] = total_memory_size
        memory_dict['available_size'] = available_size
        memory_dict['used_percent'] = used_percent
        memory_dict['used_size'] = used_size
        memory_dict['memory_usage'] = memory_usage
        memory_dict['spare_size'] = spare_size
        #-------------------------------------------------
        memory_dict['swap_memory_total'] = swap_memory_total
        memory_dict['swap_used_percent'] = swap_used_percent
        memory_dict['swap_used_size'] = swap_used_size
        memory_dict['swap_memory_usage'] = swap_memory_usage
        memory_dict['swap_spare_size'] = swap_spare_size
        memory_dict['swap_sin'] = swap_sin
        memory_dict['swap_sout'] = swap_sout

        return memory_dict

if __name__ == '__main__':
    m = MemoryInfo()
    memorys = m.get_memory_info()
    memory_json = json.dumps(memorys)
    print(memory_json)